<?php
namespace app\crm\controller;
use think\Loader;

class Serviceorder extends CrmController
{
    /**
     * 订单列表界面
     */
    public function orderList()
    {	
		//分配增删改的权限
		$menuname = "Service Order";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];	
		$this->assign("username",$_SESSION['user_info']['username']);
        $this->assign("priv",$priv);
		$this->assign("customer_code",empty($_REQUEST["customer_code"])?0:$_REQUEST["customer_code"]);
		
		$Logistics = Loader::model('Ordermanager')->getLogisticsMode();
        $this->assign("Logistics",$Logistics);
		
		$arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);
		
        $result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result);
        $this->assign('options', $result);
		// 模板输出
        return $this->fetch('orderList');
    }

    /**
     * 获取订单列表
     */
	public function orderDataList()
	{
		$page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
		
		//需先判断传值是否为空
        $where = "(1 AND (modify_price = 'N' OR (modify_price = 'Y' AND audit_status = 'Y') )";
        $where .= empty($_REQUEST["name"])?"":" AND consignee like '%".$_REQUEST["name"]."%'";
        $where .= " AND createname = '".$_SESSION['user_info']["username"]."'";
        $where .= empty($_REQUEST["phone"])?"":" AND phone = '".$_REQUEST["phone"]."'";
		$where .= empty($_REQUEST["start_createtime"])?"":" AND createtime >= '".$_REQUEST["start_createtime"]."'";
		$where .= empty($_REQUEST["dept_id"])?"":" AND dept_id = '".$_REQUEST["dept_id"]."'";
		$where .= empty($_REQUEST["import_mark"])?"":" AND import_mark = '".$_REQUEST["import_mark"]."'";
		$where .= empty($_REQUEST["end_createtime"])?"":" AND createtime <= '".$_REQUEST["end_createtime"]."'";
        $where .= empty($_REQUEST["customer_code"])?"":" AND customer_code = '".$_REQUEST["customer_code"]."'";
		$where .= empty($_REQUEST["order_status"])?"":" AND order_status = '".$_REQUEST["order_status"]."'";
		$where .= empty($_REQUEST["pay_status"])?"":" AND pay_status = '".$_REQUEST["pay_status"]."'";
		$where .= empty($_REQUEST["shipping_status"])?"":" AND shipping_status = '".$_REQUEST["shipping_status"]."'";
		$where .= empty($_REQUEST["logistics_account"])?"":" AND logistics_account = '".$_REQUEST["logistics_account"]."'";
		$where .= empty($_REQUEST["logistics_state"])?"":" AND logistics_state = '".$_REQUEST["logistics_state"]."'";
		$where .= empty($_REQUEST["shopping_name"])?"":" AND shopping_name = '".$_REQUEST["shopping_name"]."'";
		$where .= empty($_REQUEST["order_num"])?"":" AND order_num = '".$_REQUEST["order_num"]."'";
        $where .= " )";
		
		
        $arrData = Loader::model('Serviceorder')->getOrderData($page,$limit,$where);
        echo json_encode($arrData);
	}
	
	/**
     * 添加订单界面
     */
	public function addOrder()
    {	
		//分配增删改的权限
		$menuname = "Order List";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];	
		$this->assign("username",$_SESSION['user_info']['username']);
        $this->assign("priv",$priv);
		$times = date("Y-m-d H:i:s");
		$this->assign("times",$times);
		if(!empty($_REQUEST["customer_code"])){
			$customerData = Loader::model('Customer')->getAddOrder($_REQUEST["customer_code"]);
			//print_r($customerData);die;
			//判断号码是否加密
			if($priv["encrypt_phone"] == "Y"){
				if($username != "admin"){
					$customerData["phone"] = substr($customerData["phone"],0,3)."*****".substr($customerData["phone"],-2);
					$customerData["telephone"] = substr($customerData["telephone"],0,3)."*****".substr($customerData["telephone"],-2);
				}
			}
			$this->assign("customerData",$customerData);
		}
        // 模板输出
        return $this->fetch('addOrder');
    }
	
    /**
     * 编辑订单界面
     */
	public function editOrder()
    {	
		$username = $_SESSION["user_info"]["username"];
		//分配增删改的权限
		$menuname = "Service Order";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];		
		$this->assign("username",$_SESSION['user_info']['username']);
		$this->assign("priv",$priv);
		
		$id = $_GET["id"];
		$arrData = Loader::model('Serviceorder')->getOrderDetail($id);
		//判断号码是否加密
		if($priv["encrypt_phone"] == "Y"){
			if($username != "admin"){
				$arrData["phone"] = substr($arrData["phone"],0,3)."*****".substr($arrData["phone"],-2);
				$arrData["telephone"] = substr($arrData["telephone"],0,3)."*****".substr($arrData["telephone"],-2);
			}
		}
        $this->assign("arrData",$arrData);
		$Logistics = Loader::model('Ordermanager')->getLogisticsMode();
        $this->assign("Logistics",$Logistics);
        // 模板输出
        return $this->fetch('editOrder');
    }
	
    /**
    * 编辑页面商品信息
    */
	public function orderGoods()
	{
		$arrData = Loader::model('Serviceorder')->getOrderGoods($_REQUEST["id"]);
        echo json_encode($arrData);
	}
	
	/**
    * 编辑页面订单操作记录
    */
	public function orderAction()
	{
		$arrData = Loader::model('Serviceorder')->orderAction($_REQUEST["id"]);
        echo json_encode($arrData);
	}
	
	

}

?>